<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: nengwan_03@163.com
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * 动态模型
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2014-10-14
 +------------------------------------------------------------------------------
 */
namespace Home\Model;
use Think\Model;

class GuanModel extends Model {
	
	
	private $request_key = REQUEST_KEY;//密钥
	private $request_title = REQUEST_TITLE;//名称
	private $url = INTERFACE_URL;
	private $handel = '';
	
	
	/**
     * @todo 获取学校列表接口
     * @author guanhanwen
     */
	public function getSchoolList(){
		$data = array('action'=>'getSchool');
		$data['school_ids'] = EXCLUDE_SCHOOL;
		$data_from = $this->pushData($this->callBackByAes($data));
		return $data_from;
	}
	
	
	/**
	 * @todo 获取学界接口
	 * @author guanhanwen
	 */
	public function getGradeList($school_id){
		$data = array('action'=>'getClass');
		$data['school_id'] = $school_id;
		$data_from = $this->pushData($this->callBackByAes($data));
		return $data_from;
	}

	
	/**
	 * @todo 参加活动的用户信息写入平台
	 * @author guanhanwen
	 */
	public function inputUserInfo($data_info){
		if(empty($data_info)){
			return false;
		}
		$data = array('action'=>'zcRegister');
		$data['user_phone'] = $data_info['user_phone'];
		$data['user_password'] = $data_info['user_password'];
		$data['user_name'] = $data_info['user_name'] ;
		$data['school_id'] = $data_info['school_id'];
		$data['grade_id'] = $data_info['grade_id'];
		$data['source'] = 1;
		$data_from = $this->pushData($this->callBackByAes($data));
		return $data_from;
	}
	
	
	/**
	 * @todo 通过学校ID学届ID获取学校和学届的数组
	 * @author guanhanwen
	 */
	public function zcGetSchoolGrade($school_ids,$grade_ids){
		if(empty($school_ids) || empty($grade_ids)){
			return false;
		}
		$data = array('action'=>'zcGetSchoolGrade');
		$data['school_ids'] = $school_ids;
		$data['grade_ids'] = $grade_ids;
		$data_from = $this->pushData($this->callBackByAes($data));
		return $data_from;
	}
	
	// +-----------------------------------------------------------------------------------------------------
	// | Author: guanhanwen
	// | todo: 社群用到的接口
	// +-----------------------------------------------------------------------------------------------------
	
	/**
	 * @todo 获取话题详细接口
	 * @since 2015-2-26
	 * @author guanhanwen
	 */
	public function getTopic($topic_id){
		$data = array('action'=>'getTopic');
		$data['topic_id'] = $topic_id;
		$resule = $this->pushData($this->callBackByAes($data),'Community');
		return $resule;
	}
	
	public function getTopicCommentList($topic_id,$list_last_id){
		$data = array('action'=>'getTopicCommentList');
		$data['topic_id'] = $topic_id;
		$data['list_last_id'] = $list_last_id;
		$resule = $this->pushData($this->callBackByAes($data),'Community');
		return $resule;
	}
	
	/**
	 * @todo 获取社群列表接口
	 * @since 2015-1-21
	 * @author guanhanwen
	 */
	public function getCommunityList($user_id,$s = 60){
		$data = array('action'=>'getCommunityList');
		$data['user_id'] = $user_id;
		$data['s'] = $s;
		$resule = $this->pushData($this->callBackByAes($data),'Community');
		return $resule;
	}
	
	/**
	 * @todo 加入社群
	 * @since 2015-1-21
	 * @author guanhanwen
	 */
	public function joinCommunity($user_id,$community_id,$invite_user_id = ''){
		if((int)$user_id <= 0 || (int)$community_id <=0){
			return false;
		}
		if($invite_user_id != '' && (int)$invite_user_id > 0){
			$source = 2;
		}else{
			$source = 1;
		}
		$data = array('action'=>'joinCommunity');
		$data['user_id'] = $user_id;
		$data['community_id'] = $community_id;
		$data['invite_id'] = $invite_user_id;
		$data['source'] = $source;
// dump($data);die;
		$resule = $this->pushData($this->callBackByAes($data),'Community');
		return $resule;
	}
	
	/**
	 * @todo 获取邀请函信息
	 * @since 2015-1-21
	 * @author guanhanwen
	 */
	public function invitationInfo($community_id,$user_id){
		if((int)$community_id <= 0 || (int)$user_id <= 0){
			return false;
		}
		$data = array('action'=>'checkUserCommunity');
		$data['user_id'] = $user_id;
		$data['community_id'] = $community_id;
		$data['school_ids'] = EXCLUDE_SCHOOL;
		$resule = $this->pushData($this->callBackByAes($data),'Community');
		return $resule;
	}
	
	/**
	 * @todo 获取邀请列表
	 * @since 2015-2-5
	 * @author guanhanwen
	 */
	public function getUserInviteList($community_id,$user_id,$list_last_id){
		if((int)$community_id <= 0 || (int)$user_id <= 0){
			return false;
		}
		$data = array('action'=>'getUserInviteList');
		$data['user_id'] = $user_id;
		$data['community_id'] = $community_id;
		$data['list_last_id'] = $list_last_id;
		$data['s_all'] = 1;//0为认证过的学校，1为不限制
// dump($data);die;
		$resule = $this->pushData($this->callBackByAes($data),'Community');
		return $resule;
	}
	
	/**
	 * @todo 登录接口
	 * @since 2015-1-21
	 * @author guanhanwen
	 */
	public function loginUserV2($user_phone,$user_password){
		if($user_phone == '' || $user_password == ''){
			return false;
		}
		$data = array('action'=>'loginUserV2');
		$data['user_login'] = $user_phone;
		$data['user_pwd'] = $user_password;
// dump();die;
		$resule = $this->pushData($this->callBackByAes($data));
		return $resule;
	}
	
	/**
	 * @todo 推送接口
	 * @since 2015-2-2
	 * @author guanhanwen
	 */
	public function publicPushNotice($user_id,$title,$info,$action){
		$data = array('action'=>'publicPushNotice');
		$data['user_ids'] = (string)$user_id;
		$data['title'] = (string)$title;
		$data['info'] = (string)$info;
		$data['code'] = (string)$action;
		$data['extended'] = array();
		// 		dump($data);
		// return $this->callBackByAes($data);
		$resule = $this->pushData($this->callBackByAes($data));
		return $resule;
	}
	
	/**
	 * @todo 找回密码
	 * @since 2015-2-3
	 * @author guanhanwen
	 */
	public function sendPasswordV3($phone,$password){
		$data = array('action'=>'sendPasswordV3');
		$data['user_login'] = $phone;
		$data['password'] = $password;
		$resule = $this->pushData($this->callBackByAes($data));
		return $resule;
	}
	
	/**
	 * @todo 验证手机号是否
	 * @since 2015-2-3
	 * @author guanhanwen
	 */
	public function zcIsPhone($phone){
		$data = array('action'=>'zcIsPhone');
		$data['user_phone'] = $phone;
		$resule = $this->pushData($this->callBackByAes($data));
		return $resule;
	}
	
	/**
	 * @todo 获取用户信息
	 * @since 2015-2-11
	 * @author guanhanwen
	 */
	public function getUserInfo($id){
		$data = array('action'=>'dreamUserInfo');
		$data['user_id'] = $id;
		$data_from = $this->pushData($this->callBackByAes($data));
		return $data_from;
	}
	
	/**
	 * @todo 请求接口
	 */
	private function pushData(&$json_data,$interfaceAction = 'Index'){
		import('Common.Org.Msj.Curl');
		$curl =  new \Curl();
		return $this->deData($curl->curl_post($this->url.$interfaceAction,array('para'=>$json_data)));
	}
	
	/**
	 * @todo 获取验证码接口
	 */
	public function zcGetVerifyCode($phone){
		$data = array('action'=>'zcGetVerifyCode');
		$data['phone'] = $phone;
		$data_from = $this->pushData($this->callBackByAes($data));
		return $data_from;
	}
	
	/**
	 * @todo 解密接口
	 */
	private function deData(&$json_data){
		if(!is_object($this->handel)){
			import('Org.Util.Aes','','.php');
			$this->handel	=	new \Aes();
		}
		return json_decode ( urldecode ( $this->handel->siyuan_aes_decode ( str_replace ( " ", "+", $json_data ) ) ), true );
	}
	
	/**
	 * @todo	生成经过Aes加密后的字符串
	 * @param  array $json_data
	 * @return string
	 * @Author: PHP@妖孽 <dino_ma@163.com>
	 *
	 *
	 * @example
	 * success::
	 * array(
	 *    'status'=>1,
	 *    'data'=>array(),
	 *   )
	 *
	 * error:
	 * array(
	 * 	'status'=>0,
	 * 	'info'=>'错误提示信息',
	 * )
	 */
	private function callBackByAes(&$return_data){

		if(!is_object($this->handel)){
			import('Common.Org.Util.Aes','','.php');
			$this->handel = new \Aes();
		}
// 		$return_data['request_key'] = $this->request_key;
// 		$return_data['request_title'] = $this->request_title;
// 		$return_data['key'] = data_auth_sign($return_data);
// dump($this->handel->siyuan_aes_encode(urldecode(json_encode( $return_data ))));die;
		return str_replace('"','',$this->handel->siyuan_aes_encode(urldecode(json_encode( $return_data ))));
	}
}